using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EuropeEngulfed.NET
{
    //TODO Re-engineer dialog to minimize callback to game controller
    //TODO Add arrows
    //TODO Add stats and dice information
    partial class StrategicBombingWarfareDialog : Form
    {
        private GameController.playerType player;
        private GameController currentGame;
        private bool upgradedEscorts = false;
        private bool upgradeBombers = false;
        private int numEscortsShotDown = 0;
        private int numInterceptorsShotDownByEscorts = 0;
        private int numInterceptorsShotDownByBombers = 0;
        private int numBombersShotDownByInterceptors = 0;
        private int numBombersShotDownByFlak = 0;
        private int numStrategicBombHits = 0;
        private bool stratBombingDone = false;
        private bool interceptionDone = false;
        uint originalWesternAlliedBombers;
        uint originalWesternAlliedFighters;
        uint WesternAlliedFlak;
        uint originalGermanBombers;
        uint originalGermanFighters;
        uint GermanFlak;
        uint targetedWERPProduction;
        GameState.Weather currentWesternWeather;
        GameTurn.GameDate currentGameDate;
        GameTurn.GamePhase currentGamePhase;
        private Bitmap crossHairs;
        private Random r;

        public StrategicBombingWarfareDialog(GameController.playerType player,GameController currentGame,
            uint originalWesternAlliedBombers,uint originalWesternAlliedFighters,uint WesternAlliedFlak,uint originalGermanBombers,
            uint originalGermanFighters,uint GermanFlak,uint targetedWERPProduction,GameState.Weather currentWesternWeather,
            GameTurn.GameDate currentGameDate,GameTurn.GamePhase currentGamePhase,
            bool upgradedEscorts,bool upgradeBombers, bool lastTurnOperationOverlordLaunched, Random r)
        {
            System.Reflection.Assembly thisExe;
            thisExe = System.Reflection.Assembly.GetExecutingAssembly();
            System.IO.Stream file;
            file = thisExe.GetManifestResourceStream("EuropeEngulfed.NET.Resources.Target.bmp");
            crossHairs = (Bitmap)Bitmap.FromStream(file);
            crossHairs.MakeTransparent(System.Drawing.Color.White);
            file.Dispose();
            this.originalWesternAlliedBombers = originalWesternAlliedBombers;
            this.originalWesternAlliedFighters = originalWesternAlliedFighters;
            this.WesternAlliedFlak = WesternAlliedFlak;
            this.originalGermanBombers = originalGermanBombers;
            this.originalGermanFighters = originalGermanFighters;
            this.GermanFlak = GermanFlak;
            this.targetedWERPProduction = targetedWERPProduction;
            this.currentWesternWeather = currentWesternWeather;
            this.currentGameDate = currentGameDate;
            this.player = player;
            this.currentGame = currentGame;
            this.currentGamePhase = currentGamePhase;
            this.r = r;
            InitializeComponent();

            if(player == GameController.playerType.AlliedPlayer)
            {
                this.upgradedEscorts = upgradedEscorts;
                this.upgradeBombers = upgradeBombers;

                /*BOMBING IN SUPPORT OF GROUND OPERATIONS: On
                any turn(s) in which the Western-Allies have at least a 2:1 superiority
                in Fighter Points during the Strategic Warfare portion of an Axis Production
                Phase, the Allies may announce before the bombing raid is
                resolved that they want bomb hits to affect German Strat-moves in the
                west rather than German WERPS*/
                if (originalWesternAlliedFighters >= (originalGermanFighters * 2) && !lastTurnOperationOverlordLaunched)
                    this.operationOverlordCheckBox.Enabled = true;
                else
                {
                    this.operationOverlordCheckBox.Visible = false;
                    this.operationOverlordCheckBox.Checked = false;
                }

                //Bomber Display
                this.BombersPictureBox.Image = PlayingPieceGraphics.GetWesternAlliedBomberImage();
                this.numberBombersLabel.Text = originalWesternAlliedBombers.ToString();

                //Escort Display
                //TODO Show Upgraded escort image here
                this.EscortsPictureBox.Image = PlayingPieceGraphics.GetWesternAlliedFighterImage();
                this.numberEscortsLabel.Text = originalWesternAlliedFighters.ToString();

                //Interceptor Display
                this.InterceptorsPictureBox.Image = PlayingPieceGraphics.GetGermanFighterImage();
                this.numberInterceptorsLabel.Text = originalGermanFighters.ToString();

                //Flak Display
                this.FlakPictureBox.Image = PlayingPieceGraphics.GetGermanFlakImage();
                this.numberFlakLabel.Text = GermanFlak.ToString();

                this.numberWERPSLabel.Text = targetedWERPProduction.ToString();
            }
            else
            {
                this.operationOverlordCheckBox.Visible = false;
                this.operationOverlordCheckBox.Checked = false;
                //Bomber Display
                this.BombersPictureBox.Image = PlayingPieceGraphics.GetGermanBomberImage();
                this.numberBombersLabel.Text = originalGermanBombers.ToString();

                //Escort Display
                this.EscortsPictureBox.Image = PlayingPieceGraphics.GetGermanFighterImage();
                this.numberEscortsLabel.Text = originalGermanFighters.ToString();

                //Interceptor Display
                this.InterceptorsPictureBox.Image = PlayingPieceGraphics.GetWesternAlliedFighterImage();
                this.numberInterceptorsLabel.Text = originalWesternAlliedFighters.ToString();

                //Flak Display
                this.FlakPictureBox.Image = PlayingPieceGraphics.GetWesternAlliedFlakImage();
                this.numberFlakLabel.Text = WesternAlliedFlak.ToString();

                this.numberWERPSLabel.Text = targetedWERPProduction.ToString();
            }
        }

        public StrategicBombingWarfareDialog(GameController.playerType player, GameController currentGame,
        uint originalWesternAlliedBombers, uint originalWesternAlliedFighters, uint WesternAlliedFlak, uint originalGermanBombers,
        uint originalGermanFighters, uint GermanFlak, uint targetedWERPProduction, GameState.Weather currentWesternWeather,
        GameTurn.GameDate currentGameDate, GameTurn.GamePhase currentGamePhase,
        bool upgradedEscorts, bool upgradeBombers,
        bool operationOverlord, int numEscortsShotDown, int numInterceptorsShotDownByEscorts, int numInterceptorsShotDownByBombers, int numBombersShotDownByInterceptors, int numBombersShotDownByFlak, int numStrategicBombHits)
        {
            System.Reflection.Assembly thisExe;
            thisExe = System.Reflection.Assembly.GetExecutingAssembly();
            System.IO.Stream file;
            file = thisExe.GetManifestResourceStream("EuropeEngulfed.NET.Resources.Target.bmp");
            crossHairs = (Bitmap)Bitmap.FromStream(file);
            crossHairs.MakeTransparent(System.Drawing.Color.White);
            file.Dispose();
            this.originalWesternAlliedBombers = originalWesternAlliedBombers;
            this.originalWesternAlliedFighters = originalWesternAlliedFighters;
            this.WesternAlliedFlak = WesternAlliedFlak;
            this.originalGermanBombers = originalGermanBombers;
            this.originalGermanFighters = originalGermanFighters;
            this.GermanFlak = GermanFlak;
            this.targetedWERPProduction = targetedWERPProduction;
            this.currentWesternWeather = currentWesternWeather;
            this.currentGameDate = currentGameDate;
            this.player = player;
            this.currentGame = currentGame;
            this.currentGamePhase = currentGamePhase;
            InitializeComponent();

            this.StrategicBombingButton.Enabled = false;
            this.StrategicBombingButton.Visible = false;
            if (player == GameController.playerType.AlliedPlayer)
            {
                this.upgradedEscorts = upgradedEscorts;
                this.upgradeBombers = upgradeBombers;

                /*BOMBING IN SUPPORT OF GROUND OPERATIONS: On
                any turn(s) in which the Western-Allies have at least a 2:1 superiority
                in Fighter Points during the Strategic Warfare portion of an Axis Production
                Phase, the Allies may announce before the bombing raid is
                resolved that they want bomb hits to affect German Strat-moves in the
                west rather than German WERPS*/
                
                this.operationOverlordCheckBox.Enabled = false;
                if (operationOverlord)
                {
                    this.operationOverlordCheckBox.Visible = true;
                    this.operationOverlordCheckBox.Checked = true;
                }

                //Bomber Display
                this.BombersPictureBox.Image = PlayingPieceGraphics.GetWesternAlliedBomberImage();
                this.numberBombersLabel.Text = originalWesternAlliedBombers.ToString();

                //Escort Display
                //TODO Show Upgraded escort image here
                this.EscortsPictureBox.Image = PlayingPieceGraphics.GetWesternAlliedFighterImage();
                this.numberEscortsLabel.Text = originalWesternAlliedFighters.ToString();

                //Interceptor Display
                this.InterceptorsPictureBox.Image = PlayingPieceGraphics.GetGermanFighterImage();
                this.numberInterceptorsLabel.Text = originalGermanFighters.ToString();

                //Flak Display
                this.FlakPictureBox.Image = PlayingPieceGraphics.GetGermanFlakImage();
                this.numberFlakLabel.Text = GermanFlak.ToString();

                this.numberWERPSLabel.Text = targetedWERPProduction.ToString();

                //Escort Display
                this.EscortFirePictureBox.Image = PlayingPieceGraphics.GetWesternAlliedFighterImage();
                this.EscortFireResultPictureBox.Image = PlayingPieceGraphics.GetWesternAlliedFighterImage();
                this.numberOfEscortsFireLabel.Text = originalWesternAlliedFighters.ToString();
                this.numEscortsLeftLabel.Text = (originalWesternAlliedFighters - numEscortsShotDown).ToString();

                //Interceptor Display
                this.interceptorFiredUponPictureBox.Image = PlayingPieceGraphics.GetGermanFighterImage();
                this.InterceptorsLeftPictureBox.Image = PlayingPieceGraphics.GetGermanFighterImage();
                this.numInterceptorFiredUponLabel.Text = originalGermanFighters.ToString();
                this.numInterceptorsLeftLabel.Text = (originalGermanFighters - numInterceptorsShotDownByEscorts).ToString();

                //Interceptor Display
                this.InterceptorsFirePictureBox.Image = PlayingPieceGraphics.GetGermanFighterImage();
                this.InterceptorsLeftFromInterceptorFirePictureBox.Image = PlayingPieceGraphics.GetGermanFighterImage();
                this.numInterceptorFiringLabel.Text = (originalGermanFighters - numInterceptorsShotDownByEscorts).ToString();
                this.numInterceptorsLeftFromFireLabel.Text = (originalGermanFighters - numInterceptorsShotDownByEscorts - numInterceptorsShotDownByBombers).ToString();

                //Bomber Display
                this.BombersFiredUponPictureBox.Image = PlayingPieceGraphics.GetWesternAlliedBomberImage();
                this.BombersLeftFromInterceptorFirePictureBox.Image = PlayingPieceGraphics.GetWesternAlliedBomberImage();
                this.numBombersFiredUponLabel.Text = originalWesternAlliedBombers.ToString();
                this.numBombersLeftAfterInterceptorFireLabel.Text = (originalWesternAlliedBombers - numBombersShotDownByInterceptors).ToString();

                //Flak Display
                this.FlakFirePictureBox.Image = PlayingPieceGraphics.GetGermanFlakImage();
                this.numFlakFiringLabel.Text = GermanFlak.ToString();

                //Bomber Display
                this.BombersFiredUponByFlakPictureBox.Image = PlayingPieceGraphics.GetWesternAlliedBomberImage();
                this.BombersLeftFromFlakFirePictureBox.Image = PlayingPieceGraphics.GetWesternAlliedBomberImage();
                this.numBombersFiredUponByFlakLabel.Text = (originalWesternAlliedBombers - numBombersShotDownByInterceptors).ToString();
                this.numBombersLeftAfterFlakFireLabel.Text = (originalWesternAlliedBombers - numBombersShotDownByInterceptors - numBombersShotDownByFlak).ToString();

                //Bomber Display
                this.BombersFirePictureBox.Image = PlayingPieceGraphics.GetWesternAlliedBomberImage();
                this.numBombersFiringLabel.Text = (originalWesternAlliedBombers - numBombersShotDownByInterceptors - numBombersShotDownByFlak).ToString();
                if (operationOverlordCheckBox.Checked)
                    this.numWERPsFiredUponLabel.Text = "";
                else
                    this.numWERPsFiredUponLabel.Text = targetedWERPProduction.ToString();

                if (operationOverlordCheckBox.Checked)
                    this.numWERPsLeftAfterLabel.Text = "";
                else
                    this.numWERPsLeftAfterLabel.Text = (targetedWERPProduction - numStrategicBombHits).ToString();
            }
            else
            {
                this.operationOverlordCheckBox.Visible = false;
                this.operationOverlordCheckBox.Checked = false;
                //Bomber Display
                this.BombersPictureBox.Image = PlayingPieceGraphics.GetGermanBomberImage();
                this.numberBombersLabel.Text = originalGermanBombers.ToString();

                //Escort Display
                this.EscortsPictureBox.Image = PlayingPieceGraphics.GetGermanFighterImage();
                this.numberEscortsLabel.Text = originalGermanFighters.ToString();

                //Interceptor Display
                this.InterceptorsPictureBox.Image = PlayingPieceGraphics.GetWesternAlliedFighterImage();
                this.numberInterceptorsLabel.Text = originalWesternAlliedFighters.ToString();

                //Flak Display
                this.FlakPictureBox.Image = PlayingPieceGraphics.GetWesternAlliedFlakImage();
                this.numberFlakLabel.Text = WesternAlliedFlak.ToString();

                this.numberWERPSLabel.Text = targetedWERPProduction.ToString();

                //Escort Display
                this.EscortFirePictureBox.Image = PlayingPieceGraphics.GetGermanFighterImage();
                this.EscortFireResultPictureBox.Image = PlayingPieceGraphics.GetGermanFighterImage();
                this.numberOfEscortsFireLabel.Text = originalGermanFighters.ToString();
                this.numEscortsLeftLabel.Text = (originalGermanFighters - numEscortsShotDown).ToString();

                //Interceptor Display
                this.interceptorFiredUponPictureBox.Image = PlayingPieceGraphics.GetWesternAlliedFighterImage();
                this.InterceptorsLeftPictureBox.Image = PlayingPieceGraphics.GetWesternAlliedFighterImage();
                this.numInterceptorFiredUponLabel.Text = originalWesternAlliedFighters.ToString();
                this.numInterceptorsLeftLabel.Text = (originalWesternAlliedFighters - numInterceptorsShotDownByEscorts).ToString();

                //Interceptor Display
                this.InterceptorsFirePictureBox.Image = PlayingPieceGraphics.GetWesternAlliedFighterImage();
                this.InterceptorsLeftFromInterceptorFirePictureBox.Image = PlayingPieceGraphics.GetWesternAlliedFighterImage();
                this.numInterceptorFiringLabel.Text = (originalWesternAlliedFighters - numInterceptorsShotDownByEscorts).ToString();
                this.numInterceptorsLeftFromFireLabel.Text = (originalWesternAlliedFighters - numInterceptorsShotDownByEscorts).ToString();

                //Bomber Display
                this.BombersFiredUponPictureBox.Image = PlayingPieceGraphics.GetGermanBomberImage();
                this.BombersLeftFromInterceptorFirePictureBox.Image = PlayingPieceGraphics.GetGermanBomberImage();
                this.numBombersFiredUponLabel.Text = originalGermanBombers.ToString();
                this.numBombersLeftAfterInterceptorFireLabel.Text = (originalGermanBombers - numBombersShotDownByInterceptors).ToString();

                //Flak Display
                this.FlakFirePictureBox.Image = PlayingPieceGraphics.GetWesternAlliedFlakImage();
                this.numFlakFiringLabel.Text = WesternAlliedFlak.ToString();

                //Bomber Display
                this.BombersFiredUponByFlakPictureBox.Image = PlayingPieceGraphics.GetGermanBomberImage();
                this.BombersLeftFromFlakFirePictureBox.Image = PlayingPieceGraphics.GetGermanBomberImage();
                this.numBombersFiredUponByFlakLabel.Text = (originalGermanBombers - numBombersShotDownByInterceptors).ToString();
                this.numBombersLeftAfterFlakFireLabel.Text = (originalGermanBombers - numBombersShotDownByInterceptors - numBombersShotDownByFlak).ToString();

                //Bomber Display
                this.BombersFirePictureBox.Image = PlayingPieceGraphics.GetGermanBomberImage();
                this.numBombersFiringLabel.Text = (originalGermanBombers - numBombersShotDownByInterceptors - numBombersShotDownByFlak).ToString();
                this.numWERPsFiredUponLabel.Text = targetedWERPProduction.ToString();
                this.numWERPsLeftAfterLabel.Text = (targetedWERPProduction - numStrategicBombHits).ToString();
            }
            this.numEscortsHitsLabel.Text = (numInterceptorsShotDownByEscorts - numEscortsShotDown).ToString();
            this.numEscortsExchangeLabel.Text = numEscortsShotDown.ToString();
            this.escortsFireGroupBox.Visible = true;
            this.numInterceptorsHitsLabel.Text = (numBombersShotDownByInterceptors - numInterceptorsShotDownByBombers).ToString();
            this.numInterceptorsExchangeLabel.Text = numInterceptorsShotDownByBombers.ToString();
            this.InterceptorsFireGroupBox.Visible = true;
            this.numFlakHitsLabel.Text = numBombersShotDownByFlak.ToString();
            this.FlakFireGroupBox.Visible = true;

            this.ContinueAfterFlakFireButton.Enabled = false;
            this.numBombHitsLabel.Text = numStrategicBombHits.ToString();
            this.StrategicBombingGroupBox.Visible = true;
            this.closeButton.Enabled = true;
        }

        private void strategicWarfareGroupBox_Paint(object sender, PaintEventArgs e)
        {
            PlayingPieceGraphics.PaintWeatherMarker(e.Graphics, currentWesternWeather, new Point(45, 60));
        }

        //2.22b Strategic Air War
        private void ResolveEscortsFire()
        {
            if(upgradedEscorts)
            {
                int numHits = 0;
                int numExchanges = 0;
                int numEscorts = (int)originalWesternAlliedFighters;
                int numInterceptors = (int)originalGermanFighters;
                for (uint i = 0; i < numEscorts/*Only Western Allied Fighters can be upgraded*/; i++)
                {
                    //2.24b Strategic Air War
                    uint dieRoll = (uint)(r.Next(6) + 1);
                    if (dieRoll == 6)
                        numHits++;
                    else if(dieRoll == 5)
                    {
                        numExchanges++;
                    }
                }
                numInterceptors = Math.Max(0, (numInterceptors - numHits));
                numExchanges = Math.Min(numInterceptors, numInterceptors);
                numInterceptors = Math.Max(0, (numInterceptors - numExchanges));
                numEscorts = Math.Max(0, (numEscorts - numExchanges));
                numEscortsShotDown = (int)(originalWesternAlliedFighters - numEscorts);
                numInterceptorsShotDownByEscorts = (int)(originalGermanFighters - numInterceptors);
            }
            else
            {
                uint numberOfEscorts;
                uint numInterceptors;
                if (player == GameController.playerType.AxisPlayer)
                {
                    numberOfEscorts = originalGermanFighters;
                    numInterceptors = originalWesternAlliedFighters;
                }
                else
                {
                    numberOfEscorts = originalWesternAlliedFighters;
                    numInterceptors = originalGermanFighters;
                }
                for(int i=0;i<numberOfEscorts;i++)
                {
                    uint dieRoll = (uint)(r.Next(6) + 1);
                    if(dieRoll == 6)
                    {
                        numEscortsShotDown++;
                        numInterceptorsShotDownByEscorts++;
                    }
                    if (numInterceptorsShotDownByEscorts >= numInterceptors)
                        break;
                }
            }
        }

        private bool InterceptionToBeResolved()
        {
            if ((player == GameController.playerType.AlliedPlayer && originalGermanFighters > 0)
                || (player == GameController.playerType.AxisPlayer && originalWesternAlliedFighters > 0))
            {
                return currentGame.DetermineStrategicWarInterception(player == GameController.playerType.AlliedPlayer ? GameController.playerType.AxisPlayer : GameController.playerType.AlliedPlayer);
            }
            return false;
        }

        private void StrategicBombingButton_Click(object sender, EventArgs e)
        {
            stratBombingDone = true;
            eyeButton.Enabled = false;
            if (InterceptionToBeResolved())
            {
                interceptionDone = true;
                #region Resolve Interception
                ResolveEscortsFire();
                if (player == GameController.playerType.AlliedPlayer)
                {
                    //Escort Display
                    this.EscortFirePictureBox.Image = PlayingPieceGraphics.GetWesternAlliedFighterImage();
                    this.EscortFireResultPictureBox.Image = PlayingPieceGraphics.GetWesternAlliedFighterImage();
                    this.numberOfEscortsFireLabel.Text = originalWesternAlliedFighters.ToString();
                    this.numEscortsLeftLabel.Text = (originalWesternAlliedFighters - numEscortsShotDown).ToString();

                    //Interceptor Display
                    this.interceptorFiredUponPictureBox.Image = PlayingPieceGraphics.GetGermanFighterImage();
                    this.InterceptorsLeftPictureBox.Image = PlayingPieceGraphics.GetGermanFighterImage();
                    this.numInterceptorFiredUponLabel.Text = originalGermanFighters.ToString();
                    this.numInterceptorsLeftLabel.Text = (originalGermanFighters - numInterceptorsShotDownByEscorts).ToString();
                }
                else
                {
                    //Escort Display
                    this.EscortFirePictureBox.Image = PlayingPieceGraphics.GetGermanFighterImage();
                    this.EscortFireResultPictureBox.Image = PlayingPieceGraphics.GetGermanFighterImage();
                    this.numberOfEscortsFireLabel.Text = originalGermanFighters.ToString();
                    this.numEscortsLeftLabel.Text = (originalGermanFighters - numEscortsShotDown).ToString();

                    //Interceptor Display
                    this.interceptorFiredUponPictureBox.Image = PlayingPieceGraphics.GetWesternAlliedFighterImage();
                    this.InterceptorsLeftPictureBox.Image = PlayingPieceGraphics.GetWesternAlliedFighterImage();
                    this.numInterceptorFiredUponLabel.Text = originalWesternAlliedFighters.ToString();
                    this.numInterceptorsLeftLabel.Text = (originalWesternAlliedFighters - numInterceptorsShotDownByEscorts).ToString();
                }
                this.numEscortsHitsLabel.Text = (numInterceptorsShotDownByEscorts - numEscortsShotDown).ToString();
                this.numEscortsExchangeLabel.Text = numEscortsShotDown.ToString();
                this.escortsFireGroupBox.Visible = true;
                this.closeButton.Enabled = false;
                this.StrategicBombingButton.Enabled = false;
                this.ContinueAfterEscortFireButton.Visible = true;
                #endregion
            }
            else
            {
                DoFlakFire();
            }
        }

        private void ResolveInterceptorFire()
        {
            if(player == GameController.playerType.AlliedPlayer)
            {
                int hits = 0;
                int exchanges = 0;
                int numGermanInterceptorsLeft = (int)originalGermanFighters - numInterceptorsShotDownByEscorts;
                int numAlliedBombers = (int)originalWesternAlliedBombers;
                for(int i=0;i<numGermanInterceptorsLeft;i++)
                {
                    uint dieRoll = (uint)(r.Next(6) + 1);
                    if(dieRoll == 6)
                    {
                        hits++;
                    }
                    else if(dieRoll == 5)
                    {
                        if (upgradeBombers)
                            exchanges++;
                        else
                            hits++;
                    }
                }
                numAlliedBombers = Math.Max(0, numAlliedBombers - hits);
                exchanges = Math.Min(numAlliedBombers, exchanges);
                numGermanInterceptorsLeft = Math.Max(0, numGermanInterceptorsLeft - exchanges);
                numAlliedBombers = Math.Max(0, numAlliedBombers - exchanges);
                numBombersShotDownByInterceptors = (int)(originalWesternAlliedBombers - numAlliedBombers);
                numInterceptorsShotDownByBombers = (int)((originalGermanFighters - numInterceptorsShotDownByEscorts) - numGermanInterceptorsLeft);
            }
            else
            {
                uint numWAlliedInterceptorsLeft = (uint)(originalWesternAlliedFighters - numInterceptorsShotDownByEscorts);
                for (int i = 0; i < numWAlliedInterceptorsLeft; i++)
                {
                    uint dieRoll = (uint)(r.Next(6) + 1);
                    if (dieRoll == 6 || dieRoll == 5)
                    {
                        numBombersShotDownByInterceptors++;
                    }
                }

                if (numBombersShotDownByInterceptors > originalGermanBombers)
                    numBombersShotDownByInterceptors = (int)originalGermanBombers;
            }
        }

        private void ContinueAfterEscortFireButton_Click(object sender, EventArgs e)
        {
            ResolveInterceptorFire();
            if (player == GameController.playerType.AlliedPlayer)
            {
                //Interceptor Display
                this.InterceptorsFirePictureBox.Image = PlayingPieceGraphics.GetGermanFighterImage();
                this.InterceptorsLeftFromInterceptorFirePictureBox.Image = PlayingPieceGraphics.GetGermanFighterImage();
                this.numInterceptorFiringLabel.Text = (originalGermanFighters - numInterceptorsShotDownByEscorts).ToString();
                this.numInterceptorsLeftFromFireLabel.Text = (originalGermanFighters - numInterceptorsShotDownByEscorts - numInterceptorsShotDownByBombers).ToString();

                //Bomber Display
                this.BombersFiredUponPictureBox.Image = PlayingPieceGraphics.GetWesternAlliedBomberImage();
                this.BombersLeftFromInterceptorFirePictureBox.Image = PlayingPieceGraphics.GetWesternAlliedBomberImage();
                this.numBombersFiredUponLabel.Text = originalWesternAlliedBombers.ToString();
                this.numBombersLeftAfterInterceptorFireLabel.Text = (originalWesternAlliedBombers - numBombersShotDownByInterceptors).ToString();
            }
            else
            {
                //Interceptor Display
                this.InterceptorsFirePictureBox.Image = PlayingPieceGraphics.GetWesternAlliedFighterImage();
                this.InterceptorsLeftFromInterceptorFirePictureBox.Image = PlayingPieceGraphics.GetWesternAlliedFighterImage();
                this.numInterceptorFiringLabel.Text = (originalWesternAlliedFighters - numInterceptorsShotDownByEscorts).ToString();
                this.numInterceptorsLeftFromFireLabel.Text = (originalWesternAlliedFighters - numInterceptorsShotDownByEscorts).ToString();

                //Bomber Display
                this.BombersFiredUponPictureBox.Image = PlayingPieceGraphics.GetGermanBomberImage();
                this.BombersLeftFromInterceptorFirePictureBox.Image = PlayingPieceGraphics.GetGermanBomberImage();
                this.numBombersFiredUponLabel.Text = originalGermanBombers.ToString();
                this.numBombersLeftAfterInterceptorFireLabel.Text = (originalGermanBombers - numBombersShotDownByInterceptors).ToString();
            }
            this.numInterceptorsHitsLabel.Text = (numBombersShotDownByInterceptors - numInterceptorsShotDownByBombers).ToString();
            this.numInterceptorsExchangeLabel.Text = numInterceptorsShotDownByBombers.ToString();
            this.InterceptorsFireGroupBox.Visible = true;
            this.ContinueAfterEscortFireButton.Enabled = false;
            this.ContinueAfterInterceptorFireButton.Visible = true;
        }

        private void ResolveFlakFire()
        {
            int numDice = 0;
            uint currentFlak = 0;
            int leftOverBombers = 0;
            if (player == GameController.playerType.AlliedPlayer)
            {
                currentFlak = GermanFlak;
                leftOverBombers = (int)(originalWesternAlliedBombers - numBombersShotDownByInterceptors);
            }
            else
            {
                currentFlak = WesternAlliedFlak;
                leftOverBombers = (int)(originalGermanBombers - numBombersShotDownByInterceptors);
            }
            switch(currentFlak)
            {
                case 0:
                    numDice = 0;
                    break;
                case 1:
                    numDice = leftOverBombers / 5;
                    break;
                case 2:
                    numDice = leftOverBombers / 4;
                    break;
                case 3:
                    numDice = leftOverBombers / 3;
                    break;
                case 4:
                    numDice = leftOverBombers / 2;
                    break;
            }

            for(int i=0;i<numDice;i++)
            {
                uint dieRoll = (uint)(r.Next(6) + 1);
                if(dieRoll == 6)
                {
                    numBombersShotDownByFlak++;
                }
                if (numBombersShotDownByFlak >= leftOverBombers)
                    break;
            }
        }

        private void DoFlakFire()
        {
            if (player == GameController.playerType.AlliedPlayer)
            {
                //Flak Display
                this.FlakFirePictureBox.Image = PlayingPieceGraphics.GetGermanFlakImage();
                this.numFlakFiringLabel.Text = GermanFlak.ToString();

                //Bomber Display
                this.BombersFiredUponByFlakPictureBox.Image = PlayingPieceGraphics.GetWesternAlliedBomberImage();
                this.BombersLeftFromFlakFirePictureBox.Image = PlayingPieceGraphics.GetWesternAlliedBomberImage();
                this.numBombersFiredUponByFlakLabel.Text = (originalWesternAlliedBombers - numBombersShotDownByInterceptors).ToString();
                this.numBombersLeftAfterFlakFireLabel.Text = (originalWesternAlliedBombers - numBombersShotDownByInterceptors - numBombersShotDownByFlak).ToString();
            }
            else
            {
                //Flak Display
                this.FlakFirePictureBox.Image = PlayingPieceGraphics.GetWesternAlliedFlakImage();
                this.numFlakFiringLabel.Text = WesternAlliedFlak.ToString();

                //Bomber Display
                this.BombersFiredUponByFlakPictureBox.Image = PlayingPieceGraphics.GetGermanBomberImage();
                this.BombersLeftFromFlakFirePictureBox.Image = PlayingPieceGraphics.GetGermanBomberImage();
                this.numBombersFiredUponByFlakLabel.Text = (originalGermanBombers - numBombersShotDownByInterceptors).ToString();
                this.numBombersLeftAfterFlakFireLabel.Text = (originalGermanBombers - numBombersShotDownByInterceptors - numBombersShotDownByFlak).ToString();
            }

            this.numFlakHitsLabel.Text = numBombersShotDownByFlak.ToString();
            this.FlakFireGroupBox.Visible = true;
            this.ContinueAfterInterceptorFireButton.Enabled = false;
            this.ContinueAfterFlakFireButton.Visible = true;
        }

        private void ContinueAfterInterceptorFireButton_Click(object sender, EventArgs e)
        {
            ResolveFlakFire();
            DoFlakFire();
        }

        private void ResolveStrategicBombing()
        {
            GameState.Weather westernWeather = currentWesternWeather;
            bool goodWeatherForBombing = westernWeather == GameState.Weather.Clear || westernWeather == GameState.Weather.LightMud;
            int numBombers = 0;
            if(player == GameController.playerType.AlliedPlayer)
                numBombers = (int)(originalWesternAlliedBombers - numBombersShotDownByInterceptors - numBombersShotDownByFlak);
            else
                numBombers = (int)(originalGermanBombers - numBombersShotDownByInterceptors - numBombersShotDownByFlak);
            for(int i=0;i<numBombers;i++)
            {
                uint dieRoll = (uint)(r.Next(6) + 1);
                if (upgradeBombers)
                    dieRoll++;
                if (goodWeatherForBombing)
                    dieRoll++;
                if (dieRoll >= 6)
                    numStrategicBombHits++;
            }
        }

        private void ContinueAfterFlakFireButton_Click(object sender, EventArgs e)
        {
            ResolveStrategicBombing();
            if (player == GameController.playerType.AlliedPlayer)
            {
                //Bomber Display
                this.BombersFirePictureBox.Image = PlayingPieceGraphics.GetWesternAlliedBomberImage();
                this.numBombersFiringLabel.Text = (originalWesternAlliedBombers - numBombersShotDownByInterceptors - numBombersShotDownByFlak).ToString();
                if (operationOverlordCheckBox.Checked)
                    this.numWERPsFiredUponLabel.Text = "";
                else
                    this.numWERPsFiredUponLabel.Text = targetedWERPProduction.ToString();

                if (operationOverlordCheckBox.Checked)
                    this.numWERPsLeftAfterLabel.Text = "";
                else
                    this.numWERPsLeftAfterLabel.Text = (targetedWERPProduction - numStrategicBombHits).ToString();
            }
            else
            {
                //Bomber Display
                this.BombersFirePictureBox.Image = PlayingPieceGraphics.GetGermanBomberImage();
                this.numBombersFiringLabel.Text = (originalGermanBombers - numBombersShotDownByInterceptors - numBombersShotDownByFlak).ToString();
                this.numWERPsFiredUponLabel.Text = targetedWERPProduction.ToString();
                this.numWERPsLeftAfterLabel.Text = (targetedWERPProduction - numStrategicBombHits).ToString();
            }

            this.ContinueAfterFlakFireButton.Enabled = false;
            this.numBombHitsLabel.Text = numStrategicBombHits.ToString();
            this.StrategicBombingGroupBox.Visible = true;
            this.closeButton.Enabled = true;
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        public List<StratWarfareMove> GetListOfMoves()
        {
            List<StratWarfareMove> listToReturn = new List<StratWarfareMove>();

            if(stratBombingDone)
            {
                int originalNumBombers = 0;
                if(player == GameController.playerType.AlliedPlayer)
                    originalNumBombers = (int)originalWesternAlliedBombers;
                else
                    originalNumBombers = (int)originalGermanBombers;
                listToReturn.Add(new StratWarfareMove(currentGameDate, currentGamePhase, player, StratWarfareMove.stratWarfareMoveType.DoStratBombing, originalNumBombers));
                if(interceptionDone)
                    listToReturn.Add(new StratWarfareMove(currentGameDate, currentGamePhase, player, StratWarfareMove.stratWarfareMoveType.DoFighterIntercept, originalNumBombers));
                StratWarfareMove bombingMove = new StratWarfareMove(currentGameDate, currentGamePhase, player, StratWarfareMove.stratWarfareMoveType.StratBombingResults, originalNumBombers, numBombersShotDownByFlak, numBombersShotDownByInterceptors, numEscortsShotDown, numInterceptorsShotDownByEscorts, numInterceptorsShotDownByBombers, numStrategicBombHits);
                if(this.operationOverlordCheckBox.Checked)
                    bombingMove.OperationOverlord = true;
                listToReturn.Add(bombingMove);
            }
            return listToReturn;
        }

        private void interceptorFiredUponPictureBox_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(crossHairs, new Rectangle(0, 0, interceptorFiredUponPictureBox.Width, interceptorFiredUponPictureBox.Height));
        }

        private void BombersFiredUponPictureBox_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(crossHairs, new Rectangle(0, 0, BombersFiredUponPictureBox.Width, BombersFiredUponPictureBox.Height));
        }

        private void BombersFiredUponByFlakPictureBox_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(crossHairs, new Rectangle(0, 0, BombersFiredUponByFlakPictureBox.Width, BombersFiredUponByFlakPictureBox.Height));
        }

        private void WERPsFiredUponPictureBox_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(crossHairs, new Rectangle(0, 0, WERPsFiredUponPictureBox.Width, WERPsFiredUponPictureBox.Height));
        }

        private void operationOverlordCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if(operationOverlordCheckBox.Checked)
            {
                this.WERPsLeftPictureBox.Image = global::EuropeEngulfed.NET.Properties.Resources.railcars;
                this.WERPPictureBox.Image = global::EuropeEngulfed.NET.Properties.Resources.railcars;
                this.WERPsFiredUponPictureBox.Image = global::EuropeEngulfed.NET.Properties.Resources.railcars;
                this.WERPPictureBox.Invalidate();
            }
            else
            {
                this.WERPsLeftPictureBox.Image = global::EuropeEngulfed.NET.Properties.Resources.Industry;
                this.WERPsFiredUponPictureBox.Image = global::EuropeEngulfed.NET.Properties.Resources.Industry;
                this.WERPPictureBox.Image = global::EuropeEngulfed.NET.Properties.Resources.Industry;
                this.WERPPictureBox.Invalidate();
            }
        }

        private void eyeButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}